package cn.bearspark.api.service.main.impl;

import cn.bearspark.api.service.main.InfoService;
import cn.bearspark.api.vo.main.currency.CurrencyBaseInfo;
import cn.bearspark.api.vo.main.info.UserInfoRespVO;
import cn.bearspark.common.constant.GlobalConstant;
import cn.bearspark.common.util.ConvertBeanUtil;
import cn.bearspark.currency.dto.CurrencyBaseInfoDTO;
import cn.bearspark.currency.rpc.CurrencyRpc;
import cn.bearspark.framework.web.starter.context.BSRequestContext;
import cn.bearspark.user.dto.UserBaseInfoDTO;
import cn.bearspark.user.rpc.UserBaseRpc;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * @author f
 */
@Service
public class InfoServiceImpl implements InfoService {

    private static final Logger LOGGER = LoggerFactory.getLogger(InfoServiceImpl.class);

    @DubboReference(check = GlobalConstant.DUBBO_CHECK)
    private UserBaseRpc userBaseRpc;
    @DubboReference(check = GlobalConstant.DUBBO_CHECK)
    private CurrencyRpc currencyRpc;

    @Override
    public UserInfoRespVO userInfo() {
        Long userId = BSRequestContext.getUserId();

        // 如果用户未登录，则返回 null
        if (userId == null) {
            return null;
        }

        // 查询用户的基础数据
        UserBaseInfoDTO userBaseInfoDTO = userBaseRpc.getBaseInfo(userId);

        // 查询用户的 功德数 和 硬币数
        CurrencyBaseInfoDTO currencyBaseInfoDTO = currencyRpc.getBaseInfo(userId);

        // 返回成功的响应
        UserInfoRespVO userInfoRespVO = ConvertBeanUtil.convert(userBaseInfoDTO, UserInfoRespVO.class);
        userInfoRespVO.setCurrencyInfo(ConvertBeanUtil.convert(currencyBaseInfoDTO, CurrencyBaseInfo.class));
        return userInfoRespVO;
    }
}
